//index.js
//获取应用实例
const app = getApp()

Page({
	data: {
		g_iphone: '',
		top_img: '',
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		iocn_img: '',
		top: 0,
		left: 0,
		wrds_list: [],
		top_height: 200,
		c_height:400,
		wrds_an: '',
		name: '获取失败',
		wrds_num:0,
		detail:{
			setp:0,
			ratio:0
		},
		c_list:['0']
	},
	sub_false:function(){
		if(this.data.wrds_num==0){
			wx.showToast({
				title: '最少请填写一个新年FLAG',
				icon:'none',
				duration: 2000
			})
			return false
		}
		this.setData({
			wrds_an:'type_1',
		});
	},
	form_wrds: function(rse) {
		var a = {};
		var b = rse.detail.value;
		
		//
		if(!b.textarea && !this.data.wrds_an){
			wx.showToast({
				title: '不能为空',
				icon:'none',
				duration: 2000
			})
			return false
		}else if(b.textarea.length>32 && !this.data.wrds_an){
			wx.showToast({
				title:'FLGA太长啦~ 请控制在32个字以内',
				icon:'none',
				duration: 2000
			})
			return false
		}else if(!b.input && this.data.wrds_an){
			wx.showToast({
				title: '不能为空',
				icon:'none',
				duration: 2000
			})
			return false
		}else if(b.input.length > 8 && this.data.wrds_an){
			wx.showToast({
				title:'名字太长啦~ 请控制在8个字以内',
				icon:'none',
				duration: 2000
			})
			return false
		};
		if(this.data.wrds_an) {//结束
			this.formatTime(new Date)//time
			this.setData({
				text_value:'',
				wrds_an:'type_2',
				name:b.input,
				this_wrds:'on',
				top_height:this.data.top_height+20,
				time:this.time,
				c_height:this.data.c_height + 20
			});
		} else {//flag
			a.wrds = b.textarea;
			if(this.data.wrds_num==3){
				this.setData({
					wrds_an:'type_1'
				});
			};
			this.setData({
				wrds_list: this.data.wrds_list.concat(a),
				top_height:this.data.top_height + (a.wrds.length>16 ? 18:9),
				text_value:'',
				wrds_num:this.data.wrds_num+1,
				c_list:this.data.c_list.concat((a.wrds.length>16 ? '1':'0')),
				c_height:this.data.c_height + (a.wrds.length>16 ? 22:11),
			});
		}
	},
	camera_click: function() {
		const thas = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				const tempFilePaths = res.tempFilePaths[0];
				thas.iocn = tempFilePaths;
				thas.setData({
					iocn_img: tempFilePaths
				});
				//保存默认画像
				thas.canvasImg(tempFilePaths, thas.img_src, 'false', thas.width);
			}
		})
	},
	formatTime:function(date) {
		var year = date.getFullYear() 
		var month = date.getMonth() + 1
		var day = date.getDate() 
		var hour = date.getHours() 
		var minute = date.getMinutes() 
		var second = date.getSeconds() 
		this.time =  year+'/'+month+'/'+day+'/'+' '+hour+':'+minute+':'+second
	},
	focus:function(){
// 		setTimeout(function(){
// 			wx.pageScrollTo({
// 				  scrollTop:9999,
// 				  duration: 300
// 			})
// 		},500)
	},
	onLoad: function(options) { ///////////////////
		//组合json
		var type = options.type
		var Gender = options.Gender
		var type = type.split(","); //转成
		type.splice(-1, 1) //删除，
		this.img_src = '';
		this.iocn = '';
		this.width = '';
		const this_box = this;
		//
		app.request(app.apiDomain + "index.php?c=user&a=stepRatio", {}, function(res) {
			console.log(res.data.data)
			this_box.setData({
				detail:res.data.data
			})
		});
		//
		//
		this.formatTime(new Date)//time
		//
		if (type[0] == '1' && type[1] == '1' && type[2] == '1') {
			if (Gender == 'male') {
				this.img_src = '4';
				// this.seat_x=this_box.width*0.535;
				// this.seat_y=this_box.width*0.39;
			} else {
				this.img_src = '1'
				// 				this.seat_x=this_box.width*0.24;
				// 				this.seat_y=this_box.width*0.62;
			}
		} else if (type[0] == '3' && type[1] == '3' && type[2] == '3') {
			if (Gender == 'male') {
				this.img_src = '6'
				// 				this.seat_x=this_box.width*0.39;
				// 				this.seat_y=this_box.width*0.71;
			} else {
				this.img_src = '3'
				// 				this.seat_x=this_box.width*0.28;
				// 				this.seat_y=this_box.width*0.41;
			}
		} else {
			if (Gender == 'male') {
				this.img_src = '5'
				// 				this.seat_x=this_box.width*0.35;
				// 				this.seat_y=this_box.width*0.33;
			} else {
				this.img_src = '2'
				// 				this.seat_x=this_box.width*0.67;
				// 				this.seat_y=this_box.width*0.61;
			}
		};
		//
		this.setData({
			top_img: '../../PuBlic/img/eng_' + this.img_src + '.png',
			wrds_img: '../../PuBlic/img/eng_wrds_' + this.img_src + '.png',
			left: (this.seat_x / 2) * 0.9 + 'px',
			top: (this.seat_y / 2) * 0.9 + 'px',
		});
		
		//
		wx.getSystemInfo({
			success(res) {
				this_box.width = res.screenWidth * 2;
				this_box.canvasImg(this_box.img_src, 'false', this_box.width);
			}
		});
		
		// 查看是否授权index.php?c=user&a=stepRatio
		// console.log(wx.getStorageSync('icon'));
// 		wx.getImageInfo({ //线上转本地
// 			src: wx.getStorageSync('icon'),
// 			success(res) {
// 				this_box.iocn = res.path;
// 				this_box.setData({
// 					iocn_img: res.path
// 				});
// 				//保存默认画像
				// this_box.canvasImg(this_box.img_src, 'false', this_box.width);
// 			}
// 		})
	},
	searchBox: function(e) {
		if(!this.data.this_wrds|| !this.data.name){
			wx.showToast({
				title: '请填写完整',
				icon:'none',
				duration: 2000
			})
			return false
		};
// 		if (e.detail.value.input) {
// 			this.data.input = e.detail.value.input;
// 		} else {
// 			this.data.input = '健康快乐每一天'
// 		}
		this.canvasImg(this.img_src, 'true', this.width);
		wx.showLoading({
			title: '正在生画像',
		})
	},
	//开始绘画
	canvasImg: function(img_src, get, width) {
		// console.log(he_num)
		var height = width * (this.data.c_height/2);
		var vw = width / 100 ;
		var canvasimgbg = '';
		//
		// console.log('开始绘画',this.time)
		var context = wx.createCanvasContext('share')
		context.width = width;
		context.height = height;
		context.setFontSize(5*vw)
		context.setFillStyle('#DF0000')
		context.fillRect(0, 0, width, height);
		// context.stroke()
		//开始
		// context.drawImage(iocn, this.seat_x, this.seat_y, width * 0.15, width * 0.15);
		context.drawImage('../../PuBlic/img/eng_' + img_src + '.png', width*0.05, width*0.4, width*0.9, width * 0.866);//插入人物
		context.drawImage('../../PuBlic/img/eng_wrds_' + img_src + '.png', width*0.05, width*0.1,width*0.9, width * 0.417);//插入标题
		
		//步数
		context.setFontSize(4*vw)
		context.setFillStyle("#FFD900")
		context.setTextAlign('center')
		context.fillText('我最近一个月走了'+this.data.detail.setp+'万步，超过'+this.data.detail.ratio+'%的用户',width*0.5,width * 1.32);
		
		context.setTextAlign('left')
		context.setFontSize(4.5*vw)
		context.setFillStyle("#fff")
		//
		
		var text_box = this.data.wrds_list;
		var height_num = 0 
		// console.log(text_box)
		if(get=='true'){
			var z_num=0;
			for(var i=0; i < text_box.length;i++){
				var text = text_box[i].wrds; //这是要绘制的文本
				var chr = text.split(""); //这个方法是将一个字符串分割成字符串数组
				var temp = "";
				var row = [];
				for (var a = 0; a < chr.length; a++) {
					if (context.measureText(temp).width < width * 0.72) {
						temp += chr[a];
					} else {
						a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
						row.push(temp);
						temp = "";
					}
				}
				row.push(temp);
				for (var b = 0; b < row.length; b++) {
					console.log(z_num,(b==0 ? (this.data.c_list[i]*0.6):0),this.data.c_list),
					z_num = z_num + (b==0 ? (this.data.c_list[i]*0.6):0);
					context.fillText(row[b], width * 0.155, width * 1.47 + (b * (7 * vw)) + ((i + z_num) * (10 * vw)));
					context.drawImage('../../PuBlic/img/s_' + i + '.png', width*0.075, width * 1.425 + ((i + z_num) * (10 * vw)),width*0.06, width * 0.06);//插入tab
					height_num++
				}
			}
			
		}
		var he_num = (height_num * 0.07) + 0.1;
		
		
// 		for (var a = 0; a < text.length; a++) {
// 			context.fillText(text[a].wrds, width * 0.15, width * 1.47 + (a * (10 * vw)));
// 		}
		
		//签名档
		context.setFontSize(6*vw)
		context.setFillStyle("#FFD900")
		context.fillText(this.data.name,width*0.08,width * (1.62 + he_num));
		//time
		context.setFontSize(2.8*vw)
		context.fillText('立于'+this.time,width*0.12,width * (1.69 + he_num));
		context.fillText('扫码进入健康画像', width*0.67 ,width * (1.78 + he_num));
		context.fillText('立新年健康FLAG', width*0.67 ,width * (1.82 + he_num));
		context.drawImage('../../PuBlic/img/pen_2.png', width*0.08, width * (1.662 + he_num),width*0.03, width * 0.03);//插入标题
		context.drawImage('../../PuBlic/img/pen_4.png', width*0.072, width * (1.72 + he_num) ,width*0.32, width * 0.026);
		context.drawImage('../../PuBlic/img/logo_2.png', width*0.072, width * (1.78 + he_num) ,width*0.32, width * 0.0453);
		context.drawImage('../../PuBlic/img/ewm.png', width*0.65, width * (1.47 + he_num) ,width*0.26, width * 0.26);
		
		//绘画
		// context.draw();,img_load++
		var has=this;
// 		var gg=setInterval(function(e = has,c = img_load){
// 			console.log(c)
			// if(c==10){
		if(get=='true'){
			context.draw(true)
			setTimeout(function(e=has){
				e.getTempFilePath()
			},4000);
		}else{
			context.draw()
		}
				
// 			}
// 		},200)
	},
	//获取临时路径
	getTempFilePath: function() {
		wx.canvasToTempFilePath({
			canvasId: 'share',
			fileType:'jpg',
			quality:1,
			success: (res) => {
				console.log(res)
				//保存至相册
				wx.hideToast()
				wx.showToast()
				wx.saveImageToPhotosAlbum({
					filePath:  res.tempFilePath,
					success: (res) => {
						wx.showModal({
							title: '提示',
							content: '保存成功',
							showCancel: true
						})
					},
					fail: (err) => {
						// console.log(err)
					}
				});
				//上传
				wx.uploadFile({
					url: app.apiDomain + 'index.php?c=user&a=upload', //接口地址
					filePath: res.tempFilePath,
					name: 'image',
					formData: {
						user: 'test'
					},
					success(res) {
						var data = JSON.parse(res.data);
						// console.log(app.resourceDomain + data.data);
						app.request(app.apiDomain + 'index.php?c=user&a=setportrait', {
							portrait: data.data
						}, function(res) {
							if (res.data.status == '1') {}
						});
						// do something
					}
				});
			}
		})
	},
	onShareAppMessage: function() {
		return {
			title: '我的新年健康FLAG！',
			path : "/pages/index/index"
			// imageUrl: 'https://resource.160nurse.com/upload/miniprogram/index.jpeg'
		}
	},
})
